package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Course;
import com.example.health_life.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/courses")
    public Response<List<Course>> getCourseList(@RequestParam(required = false, defaultValue = "1") Integer status) {
        List<Course> courses = courseService.getCourseListWithAuthorByStatus(status);
        return Response.newSuccess(courses);
    }

    @GetMapping("/courses/{id}")
    public Response<Course> getCourseById(@PathVariable Integer id) {
        Course course = courseService.getCourseByIdWithAuthor(id);
        return Response.newSuccess(course);
    }
    

    @GetMapping("/courses/level/{level}")
    public Response<List<Course>> getCourseListByLevel(@PathVariable Byte level) {
        List<Course> courses = courseService.getCourseListByLevelWithAuthor(level);
        return Response.newSuccess(courses);
    }

    @PostMapping("/courses")
    public Response<Integer> addCourse(@RequestBody Course course) {
        int id = courseService.addCourse(course);
        return Response.newSuccess(id);
    }

    @DeleteMapping("/courses/{id}")
    public Response<Integer> deleteCourse(@PathVariable Integer id) {
        int result = courseService.deleteCourse(id);
        return Response.newSuccess(result);
    }

    @PutMapping("/courses/{id}")
    public Response<Integer> updateCourse(@PathVariable Integer id,
                                        @RequestParam(required = false,defaultValue="null") String title,
                                        @RequestParam(required = false) String introduce,
                                        @RequestParam(required = false) String coverImg,
                                        @RequestParam(required = false) Byte level,
                                        @RequestParam(required = false) Integer favorite,
                                        @RequestParam(required = false) Byte status) {
        int result = courseService.updateCourse(id, title, introduce, coverImg, level, favorite, status);
        return Response.newSuccess(result);
    }
}
